import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:kepu/page/animal_detail_page/animal_detail_page.dart';
import 'package:kepu/page/plant_detail_page/plant_detail_page.dart';
import 'package:kepu/widgets/error_widget.dart';
import 'package:kepu/widgets/image_with_title.dart';
import 'package:kepu/widgets/loading_widget.dart';

import 'provider/common_list_by_region_provider.dart';

class RegionCategoryResultPage extends ConsumerWidget {
  const RegionCategoryResultPage({
    super.key,
    required this.id,
    required this.categoryName,
  });

  final String categoryName;
  final int id;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    var result = ref.watch(commonListByRegionProvider(id: id));
    return Scaffold(
      appBar: AppBar(title: Text(categoryName)),
      body: result.when(
        data: (value) {
          if(value.code == 500){
            return Center(child: Text("没有查询到相关信息"),);
          }else{
            return Padding(
              padding: const EdgeInsets.all(15.0),
              child: ListView.separated(
                itemCount: value.data.length,
                itemBuilder: (context, index) {
                  return GestureDetector(
                    onTap: () {

                      //根据分类跳转 plant == true 说明是植物
                      if(value.data[index].plant){
                        Navigator.of(context).push(MaterialPageRoute(builder: (_)=>PlantDetailPage(value.data[index].id)));
                      }
                      //其余跳转到动物
                      else{
                        Navigator.of(context).push(MaterialPageRoute(builder: (_)=>AnimalDetailPage(value.data[index].id)));
                      }
                    },
                    child: ImageWithBriefInfo(
                      imageUrl: value.data[index].image,
                      title: value.data[index].name,
                      subtitle: value.data[index].description,
                    ),
                  );
                },
                separatorBuilder: (BuildContext context, int index) {
                  return Divider();
                },
              ),
            );
          }},
        error: (e, st) => errorWidget(),
        loading: () => loadWidget(context),
      ),
    );
  }
}
